Method to run a PHP script [with arguments] from the command line

  • 2020-03-31 20:16:06
  • OfStack

Create a simple text file with the following PHP code and save it as hello.php:
 
<?php 
echo "Hello from the CLI"; 
?> 

Now, try running the program at a command-line prompt by calling the CLI executable and providing the file name of the script:
# phphello PHP. PHP
Output Hello from the CLI


Use standard inputs and outputs
You can use these constants in your PHP script to accept input from the user, or to display the results of processing and computation. To better understand this, take a look at the following script (

List A) :

List A
 
<?php 
// ask for input 
fwrite(STDOUT, "Enter your name: "); 

// get input 
$name = trim(fgets(STDIN)); 

// write input back 
fwrite(STDOUT, "Hello, $name!"); 
?> 

Look what happens when you run it:
Shell> Hello PHP. PHP
Enter your name: Joe
Hello, Joe!

In this script, the fwrite() function first writes a message to the standard output device asking for the user's name. It then reads the user input from the standard input device

Take a PHP variable and it merges it into a string. This string is then printed out to a standard output device with fwrite().


-- use the command line argument
It is common practice to enter program parameters on the command line to change how it runs. You can also do this with CLI programs. The PHP CLI comes with two special variables for this purpose

Purpose: one is the $argv variable, which saves arguments passed to the PHP script as a separate array element from the command line. The other is the $argc variable, which holds the elements in the $argv array

Number.

It's easy to write a PHP script that reads $argv and handles the arguments it contains. Try the sample script in listing B to see how it works:

List B
 
<?php 
print_r($argv); 
?> 

Run this script by passing it some values, and check the output:

Shell> PHP phptest.php chocolate 276 "killer tie, dude!"
Array
([0] = > Test. The PHP
[1] = > chocolate
[2] = > 276
[3] = > Killer tie, dude!
)

As you can see from the output, the value passed to test.php automatically appears as an array element in $argv. Note that the first argument of $argvis is always

The name of the script itself.


Here's a more complex example (listing C) :

The list of C


code
 
<?php 
// check for all required arguments 
// first argument is always name of script! 
if ($argc != 4) { 
die("Usage: book.php <check-in-date> <num-nights> <room-type> "); 
} 

// remove first argument 
array_shift($argv); 

// get and use remaining arguments 
$checkin = $argv[0]; 
$nights = $argv[1]; 
$type = $argv[2]; 
echo "You have requested a $type room for $nights nights, checking in on $checkin. Thank you for your order! "; 
?> 


Here is an example of its usage:

Shell> PHP phpbook.php 21/05/2005 7 single
You have requested a single room for 7 nights, checking in on 21/05/2005. Thank You for your order!

Here, the script first checks $argc to make sure the number of arguments matches. It then extracts each argument from $argv and prints them out to the standard output

Related articles: